"use strict";

import Vue from "vue";
import config from '../config/index'

export default class Basic {
    constructor () {
        this.$config = config;
    }

    getImageInfoSync (url) {
        return new Promise(resolve => {
            uni.getImageInfo({
                src: url,
                success: resolve,
                fail: resolve
            });
        })
    }

    getFileInfoSync (url) {
        return new Promise(resolve => {
            uni.getFileInfo({
                src: url,
                success: resolve,
                fail: resolve
            });
        })
    }

    /**
     * 文件上传
     *
     * @param url
     * @param filePath
     * @param name
     * @param formData
     * @param header
     * @param onProgressUpdate 监听上传进度变化
     * @param onHeadersReceived 监听 HTTP Response Header 事件。会比请求完成事件更早,仅微信小程序平台支持
     * @param offProgressUpdate 取消监听上传进度变化事件，仅微信小程序平台支持
     * @param offHeadersReceived 取消监听 HTTP Response Header 事件，仅微信小程序平台支持
     * @returns {Promise<unknown>}
     */
    uploadFile ({url, filePath, name = "file", formData, header, onProgressUpdate, onHeadersReceived, offProgressUpdate, offHeadersReceived}) {
        const vm = this;
        const token = Vue.$global_util.Login.token;
        return new Promise((resolve, reject) => {
            const uploadTask = uni.uploadFile({
                url: url,
                filePath: filePath,
                name: name,
                formData: formData,
                header: {...header, token},
                success: async (uploadFileRes) => {
                    if (uploadFileRes.statusCode === 200) {
                        try {
                            const result = JSON.parse(uploadFileRes.data);
                            result.code = Vue.$global_util.getNumber(result.code, 0);
                            if (result.code === 199) {
                                Vue.$global_util.Login.toLogin();
                                reject()
                            } else {
                                resolve(result)
                            }
                        } catch (e) {
                            resolve({code: -1, message: '上传失败'})
                        }
                    } else {
                        resolve({code: -1, message: ''})
                    }
                },
                fail () {
                    resolve({code: -1, message: '上传失败'})
                }
            });
            typeof onProgressUpdate === "function" && uploadTask.onProgressUpdate(onProgressUpdate);
            //#ifdef MP-WEIXIN
            typeof onHeadersReceived === "function" && uploadTask.onHeadersReceived(onHeadersReceived);
            typeof offProgressUpdate === "function" && uploadTask.offProgressUpdate(offProgressUpdate);
            typeof offHeadersReceived === "function" && uploadTask.offHeadersReceived(offHeadersReceived);
            //#endif
        })
    }

    hideLoading () {
        uni.hideLoading()
    }

    bytes2Size (bytes) {
        if (bytes === 0) return '0 B';
        let k = 1024;
        let sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        let i = Math.floor(Math.log(bytes) / Math.log(k));
        return (bytes / Math.pow(k, i)).toPrecision(4) + ' ' + sizes[i];
        //toPrecision(3) 后面保留一位小数，如1.0GB
        //return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
    }
}
